package com.example.rabbitmqretry.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

@Component
@Slf4j
public class RabbitSender {

    @Value("${mq.queueBinding.queue}")
    private String queueName;
    @Value("${mq.queueBinding.exchange.name}")
    private String exchangeName;
    @Value("${mq.queueBinding.key}")
    private String key;
    @Autowired
    RabbitTemplate rabbitTemplate;

    public void send(String msg) {
        log.info("消息发送 msg = {}",msg);
        String msgId = UUID.randomUUID().toString();
        log.info("send msgId {}",msgId);
        Message message = MessageBuilder.withBody(msg.getBytes(StandardCharsets.UTF_8))
                .setHeader("msgId", msgId).build();
        rabbitTemplate.send(exchangeName,key,message);
    }
}
